/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.view.controller;

import cz.cvut.fel.taskpro.business.imanager.IGroupBusiness;
import cz.cvut.fel.taskpro.business.imanager.ISubjectBusiness;
import cz.cvut.fel.taskpro.business.imanager.IUserBusiness;
import cz.cvut.fel.taskpro.business.support.BusinessRuleException;
import cz.cvut.fel.taskpro.data.entity.GroupTaskPro;
import cz.cvut.fel.taskpro.data.entity.Kantor;
import cz.cvut.fel.taskpro.data.entity.Student;
import cz.cvut.fel.taskpro.data.entity.Subject;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 * This controller is responsible for create new study group. Controller is
 * request scope and is named as groupBean.
 *
 * @author Tom
 * @version 1.0.0. beta
 */
@ManagedBean(name = "groupBean")
@RequestScoped
public class GroupBean extends AbstractController implements Serializable {

    @EJB
    private IUserBusiness userB;
    @EJB
    private ISubjectBusiness subjectB;
    @EJB
    private IGroupBusiness groupB;
    private GroupTaskPro group;
    private String code;
    private List<Student> students;
    private List<Kantor> kantors;
    private List<Subject> subjects;
    private Subject subjectChoosen;
    private Student[] studentsChoosen;
    private Kantor selectedKantor;
    private Date startTime;
    private Date stopTime;
    private String dayOfTeach;

    /**
     * This method initialize class. Load all kantors, student and subject in
     * system.
     */
    @PostConstruct
    public void init() {
        recognizeLogin();
        students = userB.getStudentsAll();
        kantors = userB.getAllKantor();
        subjects = subjectB.showSubjects();
    }

    public GroupBean() {
    }

    /**
     * This function ask business layer for create new study group. Parameter
     * of group are taken from JSF page as simple data type.
     */
    public void createParallel() {
        try {
            group = new GroupTaskPro();
            group.setKantor(selectedKantor);
            group.setMStudents(Arrays.asList(studentsChoosen));
            group.setCode(code);
            group.setSubject(subjectChoosen);
            group.setDayOfTeach(dayOfTeach);
            group.setStartTime(startTime);
            group.setStopTime(stopTime);
            groupB.addGroup(group);
            printSuccMessage("6006", "The parallel with code " + group.getCode() + " in subuject " + subjectChoosen.getCode() + " has been added.");
            group = new GroupTaskPro();
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }
    public String getDayOfTeach() {
        return dayOfTeach;
    }

    public void setDayOfTeach(String dayOfTeach) {
        this.dayOfTeach = dayOfTeach;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getStopTime() {
        return stopTime;
    }

    public void setStopTime(Date stopTime) {
        this.stopTime = stopTime;
    }

    public Kantor getSelectedKantor() {
        return selectedKantor;
    }

    public void setSelectedKantor(Kantor selectedKantor) {
        this.selectedKantor = selectedKantor;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<Kantor> getKantors() {
        return kantors;
    }

    public void setKantors(List<Kantor> kantors) {
        this.kantors = kantors;
    }

    public GroupTaskPro getGroup() {
        return group;
    }

    public void setGroup(GroupTaskPro group) {
        this.group = group;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public Student[] getStudentsChoosen() {
        return studentsChoosen;
    }

    public void setStudentsChoosen(Student[] studentsChoosen) {
        this.studentsChoosen = studentsChoosen;
    }

    public Subject getSubjectChoosen() {
        return subjectChoosen;
    }

    public void setSubjectChoosen(Subject subjectChoosen) {
        this.subjectChoosen = subjectChoosen;
    }

    public List<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<Subject> subjects) {
        this.subjects = subjects;
    }
}
